package com.woniuxy.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.woniuxy.entity.Assignment;
import com.woniuxy.service.AssignmentService;
import com.woniuxy.utils.Result;

@RestController
@RequestMapping("/assignment")
public class AssignmentController {
	@Autowired
	private AssignmentService assignmentService;

	// 查询所有任务
	@GetMapping("/all")
	public List<Assignment> findAllAssignment() {

		List<Assignment> assignments = assignmentService.findAllAssignment();
		for (Assignment assignment : assignments) {
			if (assignment.getStatus() != null) {
				if (assignment.getStatus().equals("1")) {
					assignment.setStatus("进行中");
				} else if (assignment.getStatus().equals("2")) {
					assignment.setStatus("已完成");
				} else if (assignment.getStatus().equals("0")) {
					assignment.setStatus("失败");
				}
			}
		}
		return assignments;
	}

	// 根据状态查询任务
	@GetMapping("/findbystatus")
	public Result<Object> findByStatus(int status) {
		Result<Object> result = new Result<>();
		if (status == 1 || status == 0 || status == 2) {
			List<Assignment> assignments = assignmentService.findAssignmentByStatus(status);
			if (assignments == null) {
				result.setMessage("没有数据");
				return result;
			}

			for (Assignment assignment : assignments) {
				if (assignment.getStatus() != null) {
					if (assignment.getStatus().equals("1")) {
						assignment.setStatus("进行中");
					} else if (assignment.getStatus().equals("2")) {
						assignment.setStatus("已完成");
					} else {
						assignment.setStatus("失败");
					}
				}
			}
			result.setData(assignments);
			return result;
		}
		result.setMessage("异常");
		return result;
	}

	// 根据关键字模糊查询角色
	@GetMapping("/findbywords")
	public Result<Object> findByWords(String words) {
		Result<Object> result = new Result<>();
		List<Assignment> assignments = assignmentService.findByWords(words);
		if (assignments == null) {
			result.setMessage("没有数据");
		}
		for (Assignment assignment : assignments) {
			if (assignment.getStatus() != null) {
				if (assignment.getStatus().equals("1")) {
					assignment.setStatus("进行中");
				} else if (assignment.getStatus().equals("2")) {
					assignment.setStatus("已完成");
				} else {
					assignment.setStatus("失败");
				}
			}
		}
		result.setData(assignments);
		return result;
	}
}
